package com.wcy.service.impl;

import com.wcy.entity.Permission;
import com.wcy.entity.UserRole;
import com.wcy.mapper.PermissionMapper;
import com.wcy.mapper.RolePermissionMapper;
import com.wcy.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴传亿
 * @since 2025-04-29
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    public List<Permission> getPermissionByRoleId(Integer roleId){
        return permissionMapper.selectPermissionByRoleId(roleId);
    }

    @Override
    public List<String> getPermissionsByRoleList(List<UserRole> userRoleList) {
        Set<Permission> permissions = new HashSet<>();
        userRoleList.forEach(userRole -> {
            permissions.addAll(permissionMapper.selectPermissionByRoleId(userRole.getRoleId()));
        });
        return permissions.stream().map(Permission::getPermissionName).collect(Collectors.toList());
    }
}
